home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / cg.lha / cg / m2c / Optimize.c < prev    next >
C/C++ Source or Header  |  1992-11-24  |  12KB  |  460 lines

  1. #include "SYSTEM_.h"
  2.  
  3. #ifndef DEFINITION_System
  4. #include "System.h"
  5. #endif
  6.  
  7. #ifndef DEFINITION_IO
  8. #include "IO.h"
  9. #endif
  10.  
  11. #ifndef DEFINITION_Tree
  12. #include "Tree.h"
  13. #endif
  14.  
  15. #ifndef DEFINITION_General
  16. #include "General.h"
  17. #endif
  18.  
  19. #ifndef DEFINITION_DynArray
  20. #include "DynArray.h"
  21. #endif
  22.  
  23. #ifndef DEFINITION_IO
  24. #include "IO.h"
  25. #endif
  26.  
  27. #ifndef DEFINITION_Idents
  28. #include "Idents.h"
  29. #endif
  30.  
  31. #ifndef DEFINITION_Sets
  32. #include "Sets.h"
  33. #endif
  34.  
  35. #ifndef DEFINITION_Relations
  36. #include "Relations.h"
  37. #endif
  38.  
  39. #ifndef DEFINITION_Tree
  40. #include "Tree.h"
  41. #endif
  42.  
  43. #ifndef DEFINITION_Order
  44. #include "Order.h"
  45. #endif
  46.  
  47. #ifndef DEFINITION_Optimize
  48. #include "Optimize.h"
  49. #endif
  50.  
  51. SHORTCARD Optimize_ChildrenDyn, Optimize_ChildrenIn, Optimize_AttributeIn, Optimize_AttributeOut, Optimize_AttributeTree, Optimize_AttributeParam, Optimize_AttributeVar, Optimize_AttributeDemand, Optimize_AttributeStack;
  52. IO_tFile Optimize_yyf;
  53. PROC Optimize_Exit;
  54.  
  55. typedef struct S_1 {
  56.     SHORTCARD Birth, Death;
  57. } tLife;
  58. static Sets_tSet Children, Parents, Relevant;
  59. static SHORTCARD ClassIndex, MaxChildUse, MaxParentUse, Attr, Comp, Last, ChildsVisit, i, i2, j, j2, k, Visit, v;
  60. static Tree_tTree ActChild, ActClass, ChildsClass;
  61. static LONGINT LifeSize;
  62. static struct S_2 {
  63.     tLife A[10000 + 1];
  64. } *LifePtr;
  65. static void yyAbort ARGS((CHAR yyFunction[], LONGCARD ));
  66. static BOOLEAN yyIsEqual ARGS((BYTE yya[], LONGCARD , BYTE yyb[], LONGCARD ));
  67. struct S_5 {
  68.     union {
  69.         char dummy;
  70.     } U_1;
  71. };
  72. static void LifeTime2 ARGS((Tree_tTree t));
  73. struct S_6 {
  74.     union {
  75.         char dummy;
  76.     } U_1;
  77. };
  78. struct S_7 {
  79.     union {
  80.         char dummy;
  81.     } U_1;
  82. };
  83. static void yyExit ARGS(());
  84.  
  85.  
  86. static void yyAbort
  87. # ifdef __STDC__
  88. (CHAR yyFunction[], LONGCARD O_1)
  89. # else
  90. (yyFunction, O_1)
  91. CHAR yyFunction[];
  92. LONGCARD O_1;
  93. # endif
  94. {
  95.   OPEN_ARRAY_LOCALS
  96.  
  97.   ALLOC_OPEN_ARRAYS(O_1 * sizeof(CHAR), 1)
  98.   COPY_OPEN_ARRAY(yyFunction, O_1, CHAR)
  99.   IO_WriteS((System_tFile)IO_StdError, (STRING)"Error: module Optimize, routine ", 32L);
  100.   IO_WriteS((System_tFile)IO_StdError, yyFunction, O_1);
  101.   IO_WriteS((System_tFile)IO_StdError, (STRING)" failed", 7L);
  102.   IO_WriteNl((System_tFile)IO_StdError);
  103.   (*Optimize_Exit)();
  104.   FREE_OPEN_ARRAYS
  105. }
  106.  
  107. static BOOLEAN yyIsEqual
  108. # ifdef __STDC__
  109. (BYTE yya[], LONGCARD O_3, BYTE yyb[], LONGCARD O_2)
  110. # else
  111. (yya, O_3, yyb, O_2)
  112. BYTE yya[];
  113. LONGCARD O_3;
  114. BYTE yyb[];
  115. LONGCARD O_2;
  116. # endif
  117. {
  118.   INTEGER yyi;
  119.   OPEN_ARRAY_LOCALS
  120.  
  121.   ALLOC_OPEN_ARRAYS(O_2 * sizeof(WORD) + O_3 * sizeof(WORD), 2)
  122.   COPY_OPEN_ARRAY(yyb, O_2, WORD)
  123.   COPY_OPEN_ARRAY(yya, O_3, WORD)
  124.   {
  125.     LONGINT B_1 = 0, B_2 = (INTEGER)(O_3 - 1);
  126.  
  127.     if (B_1 <= B_2)
  128.       for (yyi = B_1;; yyi += 1) {
  129.         if (yya[yyi] != yyb[yyi]) {
  130.           FREE_OPEN_ARRAYS
  131.           return FALSE;
  132.         }
  133.         if (yyi >= B_2) break;
  134.       }
  135.   }
  136.   FREE_OPEN_ARRAYS
  137.   return TRUE;
  138. }
  139.  
  140. void Optimize_LifeTime1
  141. # ifdef __STDC__
  142. (Tree_tTree t)
  143. # else
  144. (t)
  145. Tree_tTree t;
  146. # endif
  147. {
  148.   struct S_5 yyTempo;
  149.  
  150.   if (t == Tree_NoTree) {
  151.     return;
  152.   }
  153.   if (t->U_1.V_1.Kind == Tree_Class) {
  154.     {
  155.       register Tree_yClass *W_1 = &t->U_1.V_5.Class;
  156.  
  157.       LifeSize = W_1->InstCount + 1;
  158.       DynArray_MakeArray((ADDRESS *)&LifePtr, &LifeSize, (LONGINT)sizeof(tLife));
  159.       {
  160.         SHORTCARD B_3 = 1, B_4 = W_1->InstCount;
  161.  
  162.         if (B_3 <= B_4)
  163.           for (i = B_3;; i += 1) {
  164.             LifePtr->A[i].Birth = 0;
  165.             LifePtr->A[i].Death = 0;
  166.             if (i >= B_4) break;
  167.           }
  168.       }
  169.       Visit = 1;
  170.       {
  171.         SHORTCARD B_5 = 1, B_6 = W_1->InstCount;
  172.  
  173.         if (B_5 <= B_6)
  174.           for (i = B_5;; i += 1) {
  175.             i2 = W_1->Instance->A[i - 1].Order;
  176.             {
  177.               register Tree_tInstance *W_2 = &W_1->Instance->A[i2 - 1];
  178.  
  179.               if (SET_IS_SUBSET1(SET_ELEM(Tree_Left) | SET_ELEM(Tree_Inherited) | SET_ELEM(Tree_First), W_2->Properties)) {
  180.                 Visit = W_2->Attribute->U_1.V_9.Child.Partition;
  181.               }
  182.               if (SET_IS_SUBSET1(SET_ELEM(Tree_Left) | SET_ELEM(Tree_Synthesized), W_2->Properties)) {
  183.                 LifePtr->A[i2].Birth = Visit;
  184.                 INCL(W_2->Attribute->U_1.V_9.Child.Usage, Visit);
  185.               }
  186.               if (IN(Tree_Right, W_2->Properties)) {
  187.                 LifePtr->A[i2].Birth = Visit;
  188.               }
  189.               if (SET_IS_SUBSET1(SET_ELEM(Tree_Left) | SET_ELEM(Tree_Synthesized) | SET_ELEM(Tree_First), W_2->Properties) && !IN(Tree_Dummy, W_2->Properties) || SET_IS_SUBSET1(SET_ELEM(Tree_Right) | SET_ELEM(Tree_Inherited) | SET_ELEM(Tree_First), W_2->Properties)) {
  190.                 {
  191.                   SHORTCARD B_7 = 1, B_8 = W_1->AttrCount;
  192.  
  193.                   if (B_7 <= B_8)
  194.                     for (j = B_7;; j += 1) {
  195.                       if (Relations_IsRelated((LONGINT)i2, (LONGINT)j, W_1->DP)) {
  196.                         LifePtr->A[j].Death = Visit;
  197.                         INCL(W_1->Instance->A[j - 1].Attribute->U_1.V_9.Child.Usage, Visit);
  198.                       }
  199.                       if (j >= B_8) break;
  200.                     }
  201.                 }
  202.                 {
  203.                   SHORTCARD B_9 = W_1->AttrCount + 1, B_10 = W_1->InstCount;
  204.  
  205.                   if (B_9 <= B_10)
  206.                     for (j = B_9;; j += 1) {
  207.                       if (Relations_IsRelated((LONGINT)i2, (LONGINT)j, W_1->DP)) {
  208.                         LifePtr->A[j].Death = Visit;
  209.                       }
  210.                       if (j >= B_10) break;
  211.                     }
  212.                 }
  213.               }
  214.               if (SET_IS_SUBSET1(SET_ELEM(Tree_Right) | SET_ELEM(Tree_Synthesized) | SET_ELEM(Tree_First), W_2->Properties)) {
  215.                 ActClass = t;
  216.                 ActChild = W_2->Selector;
  217.                 ChildsClass = W_2->Selector->U_1.V_9.Child.Class;
  218.                 ChildsVisit = W_2->Attribute->U_1.V_9.Child.Partition;
  219.                 LifeTime2(ChildsClass);
  220.                 Tree_ForallClasses(ChildsClass->U_1.V_5.Class.Extensions, (Tree_ProcOfT)LifeTime2);
  221.               }
  222.             }
  223.             if (i >= B_6) break;
  224.           }
  225.       }
  226.       {
  227.         SHORTCARD B_11 = 1, B_12 = W_1->AttrCount;
  228.  
  229.         if (B_11 <= B_12)
  230.           for (i = B_11;; i += 1) {
  231.             {
  232.               register Tree_tInstance *W_3 = &W_1->Instance->A[i - 1];
  233.  
  234.               if (IN(Tree_Synthesized, W_3->Properties) && LifePtr->A[i].Birth < LifePtr->A[i].Death) {
  235.                 INCL(W_3->Attribute->U_1.V_9.Child.Properties, Tree_Tree);
  236.               }
  237.             }
  238.             if (i >= B_12) break;
  239.           }
  240.       }
  241.       {
  242.         SHORTCARD B_13 = W_1->AttrCount + 1, B_14 = W_1->InstCount;
  243.  
  244.         if (B_13 <= B_14)
  245.           for (i = B_13;; i += 1) {
  246.             if (LifePtr->A[i].Birth < LifePtr->A[i].Death) {
  247.               {
  248.                 register Tree_tInstance *W_4 = &W_1->Instance->A[i - 1];
  249.  
  250.                 INCL(W_4->Attribute->U_1.V_9.Child.Properties, Tree_Tree);
  251.               }
  252.             }
  253.             if (i >= B_14) break;
  254.           }
  255.       }
  256.       DynArray_ReleaseArray((ADDRESS *)&LifePtr, &LifeSize, (LONGINT)sizeof(tLife));
  257.       return;
  258.     }
  259.   }
  260. }
  261.  
  262. static void LifeTime2
  263. # ifdef __STDC__
  264. (Tree_tTree t)
  265. # else
  266. (t)
  267. Tree_tTree t;
  268. # endif
  269. {
  270.   struct S_6 yyTempo;
  271.  
  272.   if (t == Tree_NoTree) {
  273.     return;
  274.   }
  275.   if (t->U_1.V_1.Kind == Tree_Class) {
  276.     {
  277.       register Tree_yClass *W_5 = &t->U_1.V_5.Class;
  278.  
  279.       v = 1;
  280.       {
  281.         SHORTCARD B_15 = 1, B_16 = W_5->InstCount;
  282.  
  283.         if (B_15 <= B_16)
  284.           for (j = B_15;; j += 1) {
  285.             j2 = W_5->Instance->A[j - 1].Order;
  286.             {
  287.               register Tree_tInstance *W_6 = &W_5->Instance->A[j2 - 1];
  288.  
  289.               if (SET_IS_SUBSET1(SET_ELEM(Tree_Left) | SET_ELEM(Tree_Inherited) | SET_ELEM(Tree_First), W_6->Properties)) {
  290.                 v = W_6->Attribute->U_1.V_9.Child.Partition;
  291.                 if (v > ChildsVisit) {
  292.                   return;
  293.                 }
  294.               }
  295.               if (!IN(Tree_Dummy, W_6->Properties) && ChildsVisit == v) {
  296.                 {
  297.                   SHORTCARD B_17 = 1, B_18 = ChildsClass->U_1.V_5.Class.AttrCount;
  298.  
  299.                   if (B_17 <= B_18)
  300.                     for (k = B_17;; k += 1) {
  301.                       if (Relations_IsRelated((LONGINT)j2, (LONGINT)k, W_5->DP)) {
  302.                         LifePtr->A[ActClass->U_1.V_5.Class.AttrCount + ActChild->U_1.V_9.Child.InstOffset + k].Death = Visit;
  303.                       }
  304.                       if (k >= B_18) break;
  305.                     }
  306.                 }
  307.               }
  308.             }
  309.             if (j >= B_16) break;
  310.           }
  311.       }
  312.       return;
  313.     }
  314.   }
  315. }
  316.  
  317. void Optimize_LifeTime3
  318. # ifdef __STDC__
  319. (Tree_tTree t)
  320. # else
  321. (t)
  322. Tree_tTree t;
  323. # endif
  324. {
  325.   struct S_7 yyTempo;
  326.  
  327.   if (t == Tree_NoTree) {
  328.     return;
  329.   }
  330.   if (t->U_1.V_1.Kind == Tree_Class) {
  331.     {
  332.       register Tree_yClass *W_7 = &t->U_1.V_5.Class;
  333.  
  334.       ActClass = t;
  335.       Tree_ForallAttributes(W_7->Attributes, (Tree_ProcOfT)Optimize_LifeTime3);
  336.       return;
  337.     }
  338.   }
  339.   if (t->U_1.V_1.Kind == Tree_Child) {
  340.     {
  341.       register Tree_yChild *W_8 = &t->U_1.V_9.Child;
  342.  
  343.       INCL(W_8->Properties, Tree_Tree);
  344.       if (IN(Tree_Input, W_8->Properties)) {
  345.         INC(Optimize_ChildrenIn);
  346.       } else {
  347.         INC(Optimize_ChildrenDyn);
  348.       }
  349.       return;
  350.     }
  351.   }
  352.   if (t->U_1.V_1.Kind == Tree_Attribute) {
  353.     {
  354.       register Tree_yAttribute *W_9 = &t->U_1.V_10.Attribute;
  355.  
  356.       if (Sets_IsElement(ORD('0'), &Tree_Options)) {
  357.         if (IN(Tree_Input, W_9->Properties) || IN(Tree_Output, W_9->Properties)) {
  358.           INCL(W_9->Properties, Tree_Tree);
  359.           if (IN(Tree_Input, W_9->Properties)) {
  360.             INC(Optimize_AttributeIn);
  361.           } else {
  362.             INC(Optimize_AttributeOut);
  363.           }
  364.         }
  365.         if (!IN(Tree_Tree, W_9->Properties)) {
  366.           INCL(W_9->Properties, Tree_Parameter);
  367.         }
  368.       } else {
  369.         INCL(W_9->Properties, Tree_Tree);
  370.       }
  371.       if (((SET_ELEM(Tree_Test) | SET_ELEM(Tree_Dummy) | SET_ELEM(Tree_Virtual) | SET_ELEM(Tree_Demand)) & W_9->Properties) != 0X0L) {
  372.         EXCL(W_9->Properties, Tree_Tree);
  373.         EXCL(W_9->Properties, Tree_Parameter);
  374.       }
  375.       if (Sets_IsElement(ORD('3'), &Tree_Options)) {
  376.         if (((SET_ELEM(Tree_Test) | SET_ELEM(Tree_Dummy) | SET_ELEM(Tree_Virtual) | SET_ELEM(Tree_Input) | SET_ELEM(Tree_Output)) & W_9->Properties) == 0X0L) {
  377.           Idents_WriteIdent((System_tFile)IO_StdOutput, ActClass->U_1.V_5.Class.Name);
  378.           IO_WriteS((System_tFile)IO_StdOutput, (STRING)"    = ", 3L);
  379.           Idents_WriteIdent((System_tFile)IO_StdOutput, W_9->Name);
  380.           IO_WriteS((System_tFile)IO_StdOutput, (STRING)"    ", 1L);
  381.           if (IN(Tree_Tree, W_9->Properties)) {
  382.             IO_WriteS((System_tFile)IO_StdOutput, (STRING)"Tree", 4L);
  383.             INC(Optimize_AttributeTree);
  384.           } else if (IN(Tree_Parameter, W_9->Properties)) {
  385.             IO_WriteS((System_tFile)IO_StdOutput, (STRING)"Parameter", 9L);
  386.             INC(Optimize_AttributeParam);
  387.           } else if (IN(Tree_Stack, W_9->Properties)) {
  388.             IO_WriteS((System_tFile)IO_StdOutput, (STRING)"Stack", 5L);
  389.             INC(Optimize_AttributeStack);
  390.           } else if (IN(Tree_Variable, W_9->Properties)) {
  391.             IO_WriteS((System_tFile)IO_StdOutput, (STRING)"Variable", 8L);
  392.             INC(Optimize_AttributeVar);
  393.           } else if (IN(Tree_Demand, W_9->Properties)) {
  394.             IO_WriteS((System_tFile)IO_StdOutput, (STRING)"Demand", 6L);
  395.             INC(Optimize_AttributeDemand);
  396.           }
  397.           IO_WriteNl((System_tFile)IO_StdOutput);
  398.         }
  399.       }
  400.       return;
  401.     }
  402.   }
  403. }
  404.  
  405. void Optimize_BeginOptimize
  406. # ifdef __STDC__
  407. ()
  408. # else
  409. ()
  410. # endif
  411. {
  412. }
  413.  
  414. void Optimize_CloseOptimize
  415. # ifdef __STDC__
  416. ()
  417. # else
  418. ()
  419. # endif
  420. {
  421. }
  422.  
  423. static void yyExit
  424. # ifdef __STDC__
  425. ()
  426. # else
  427. ()
  428. # endif
  429. {
  430.   IO_CloseIO();
  431.   Exit(1L);
  432. }
  433.  
  434. void BEGIN_Optimize()
  435. {
  436.   static BOOLEAN has_been_called = FALSE;
  437.  
  438.   if (!has_been_called) {
  439.     has_been_called = TRUE;
  440.  
  441.     BEGIN_IO();
  442.     BEGIN_Tree();
  443.     BEGIN_System();
  444.     BEGIN_IO();
  445.     BEGIN_Tree();
  446.     BEGIN_General();
  447.     BEGIN_DynArray();
  448.     BEGIN_IO();
  449.     BEGIN_Idents();
  450.     BEGIN_Sets();
  451.     BEGIN_Relations();
  452.     BEGIN_Tree();
  453.     BEGIN_Order();
  454.  
  455.     Optimize_yyf = IO_StdOutput;
  456.     Optimize_Exit = yyExit;
  457.     Optimize_BeginOptimize();
  458.   }
  459. }
  460.